function [m, mi_flav2_ind] = gen_micro_moment_i(id, q, dummy, ...
    price, income, weights, cutoff, cutoff_craft_inc, id_boot)

sum1 = @(x) sum(x, 1);
mean1 = @(x) mean(x, 1);

cutoff_lb = [0, cutoff];% NOTE: different from cutoff_lb and cutoff_ub in gmmobj
cutoff_ub = [cutoff, 150000];
n_cutoff = length(cutoff_lb);

id_weights = grpstats(weights, id, @mean);

dummy_q = bsxfun(@times, dummy, q);
rev = q.*price;

ttq_dummy = grpstats(dummy_q, id, sum1);
ttq = grpstats(q, id, sum1);
ttr = grpstats(rev, id, sum1);

avg_inc = grpstats(income, id, mean1);

mq_income = nan(n_cutoff, 1);
mp_income = nan(n_cutoff, 1);
n_flav = size(dummy_q, 2);

if ~isempty(id_boot)
    ttq = ttq(id_boot);
    ttq_dummy = ttq_dummy(id_boot, :);
    ttr = ttr(id_boot, :);
    avg_inc = avg_inc(id_boot, :);
    id_weights = id_weights(id_boot, :);
end

for nc = 1 : n_cutoff
    ind = (avg_inc>cutoff_lb(nc) & avg_inc<=cutoff_ub(nc));
    
    mq_income(nc) = sum(ttq(ind).*id_weights(ind))/sum(id_weights(ind));    
    mp_income(nc) = sum(ttr(ind).*id_weights(ind))/sum(ttq(ind).*id_weights(ind));    
end

mq_income2 = mq_income(2:end) - mq_income(1);
mp_income2 = mp_income(2:end) - mp_income(1);

mi_flav = nan(n_flav, length(cutoff_craft_inc));
mq_flav = nan(n_flav, 1);
for nf = 1 : n_flav
    mi_flav(nf, :) = sum((avg_inc(ttq_dummy(:, nf)>=1)<cutoff_craft_inc).*id_weights(ttq_dummy(:, nf)>=1))/sum(id_weights(ttq_dummy(:, nf)>=1));
    mq_flav(nf) = sum(ttq_dummy(ttq_dummy(:, nf)>=1, nf).*id_weights(ttq_dummy(:, nf)>=1))/sum(id_weights(ttq_dummy(:, nf)>=1));
end

mi_flav2 = mi_flav(3, :) - mean(mi_flav([1 2 4:length(mi_flav)], :), 1);
mq_flav2 = mq_flav([1 2 4:length(mq_flav)]);
mq_flav_craft = sum(ttq_dummy(ttq_dummy(:, 3)>=1, :).*id_weights(ttq_dummy(:, 3)>=1))/sum(id_weights(ttq_dummy(:, 3)>=1));

mq = sum(ttq.*id_weights)'/sum(id_weights);

m = [mq; mq_flav2; mq_flav_craft'; mq_income2; mi_flav2(:); mp_income2];

mi_flav2_ind = [length([mq; mq_flav; mq_income])+1:length([mq; mq_flav; mq_income; mi_flav2(:)])];
